import uuid
from dataclasses import dataclass
from typing import Any

import pytest
from sqlalchemy import insert
from sqlalchemy.ext.asyncio import AsyncSession

from recruit.adapter.database.gateway import ResumeContactGateway
from recruit.adapter.database.models import Resume, ResumeContact
from .fixtures.resume import ResumeTest
from .utils import check_attr_model, delete_user, check_get_one, get_resume


@dataclass
class ResumeContactTest:
    id: uuid.uuid4()
    resume_id: uuid.uuid4()
    email: str
    phone: str
    telegram: str


async def _check_resume(
        session: AsyncSession,
        data: ResumeContactTest
) -> None:
    resume_contact = await get_resume(session, ResumeContact, data.id)
    assert isinstance(resume_contact, ResumeContact)
    check_get_one(
        resume_contact,
        data,
        ('id', 'resume_id', 'email', 'phone', 'telegram')
    )


async def _create_resume_contact(
        session: AsyncSession,
        data: dict[str, Any]
) -> ResumeContactTest:
    stmt = insert(ResumeContact).values(**data).returning(ResumeContact)
    resume_contact = (await session.execute(stmt)).scalar_one()
    resume_contact = ResumeContactTest(
        id=resume_contact.id,
        resume_id=resume_contact.resume_id,
        email=resume_contact.email,
        phone=resume_contact.phone,
        telegram=resume_contact.telegram
    )
    await session.commit()
    return resume_contact


async def test_create(
        db_session: AsyncSession,
        get_resume_contact: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_contact_gateway = ResumeContactGateway(db_session)
    get_resume_contact['resume_id'] = create_resume.id
    resume_contact = await resume_contact_gateway.create(
        **get_resume_contact
    )
    assert isinstance(resume_contact.resume, Resume)
    check_attr_model(resume_contact, get_resume_contact)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)


@pytest.mark.parametrize(
    'keys', [
        ('id', 'resume_id', 'phone', 'email', 'telegram')
    ]
)
async def test_get_one(
        keys: tuple[str, ...],
        db_session: AsyncSession,
        get_resume_contact: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_contact_gateway = ResumeContactGateway(db_session)
    get_resume_contact['resume_id'] = create_resume.id
    created_resume_contact = await _create_resume_contact(
        db_session,
        get_resume_contact
    )
    for key, value in get_resume_contact.items():
        resume_contact = await resume_contact_gateway.get_one(
            **{key: value}
        )
        assert resume_contact is not None
        check_get_one(resume_contact, created_resume_contact, keys)
    await delete_user(db_session, create_resume.user_id)


@pytest.mark.parametrize(
    'data, keys', [(
            {
                'email': 'email@email.com',
                'phone': '+7 (999)-999-99-99',
                'telegram': '@username'
            },
            ('id', 'resume_id')
    )]
)
async def test_update(
        data: dict[str, Any],
        keys: tuple[str, ...],
        db_session: AsyncSession,
        get_resume_contact: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_contact_gateway = ResumeContactGateway(db_session)
    get_resume_contact['resume_id'] = create_resume.id
    created_resume_contact = await _create_resume_contact(
        db_session,
        get_resume_contact
    )
    resume_contact = await resume_contact_gateway.update(
        created_resume_contact.resume_id, **data
    )
    check_get_one(resume_contact, created_resume_contact, keys)
    check_attr_model(resume_contact, data)
    await db_session.commit()
    for key, value in data.items():
        setattr(created_resume_contact, key, value)
    await _check_resume(db_session, created_resume_contact)
    await delete_user(db_session, create_resume.user_id)


async def test_check_resume(
        db_session: AsyncSession,
        get_resume_contact: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_contact_gateway = ResumeContactGateway(db_session)
    get_resume_contact['resume_id'] = create_resume.id
    created_resume_contact = await _create_resume_contact(
        db_session,
        get_resume_contact
    )
    val = await resume_contact_gateway.check_resume(
        created_resume_contact.resume_id
    )
    assert val
    await delete_user(db_session, create_resume.user_id)
    val = await resume_contact_gateway.check_resume(
        created_resume_contact.resume_id
    )
    assert not val


async def test_delete(
        db_session: AsyncSession,
        get_resume_contact,
        create_resume: ResumeTest
) -> None:
    resume_contact_gateway = ResumeContactGateway(db_session)
    get_resume_contact['resume_id'] = create_resume.id
    created_resume_contact = await _create_resume_contact(
        db_session,
        get_resume_contact
    )
    await resume_contact_gateway.delete(created_resume_contact.resume_id)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)
    resume_contact = await get_resume(
        db_session,
        ResumeContact,
        created_resume_contact.id
    )
    assert resume_contact is None
